package com.yq.websocket.socket;

import android.util.Log;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.handshake.ServerHandshake;

import java.net.InetSocketAddress;
import java.net.URI;

public class SocketClient extends WebSocketClient {

    public SocketClient(URI serverUri) {
        super(serverUri);
    }

    private static ClientListener listener;

    @Override
    public void onOpen(ServerHandshake handShakeData) {
        Log.d("WebSocketClient", "onOpen" + "成功连接到：" + getRemoteSocketAddress());
        Log.d("WebSocketClient", "onOpen: " + handShakeData.toString());
        if (listener != null) {
            listener.onOpen(handShakeData);
        }
    }

    @Override
    public void onMessage(String message) {
        Log.d("WebSocketClient", "onMessage" + message);
        if (listener != null) {
            listener.onMessage(message);
        }

    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        Log.d("WebSocketClient", "onClose");
        if (listener != null) {
            listener.onClose(code, reason, remote);
        }
    }

    @Override
    public void onError(Exception ex) {
        Log.d("WebSocketClient", "onError:" + ex.getMessage());
        if (listener != null) {
            listener.onError(ex);
        }

    }

    private static WebSocketClient webSocketClient;

    public static boolean connect(String ip, int port) {
        if (webSocketClient != null) {
            release();
        }
        if (webSocketClient == null) {
            URI uri = URI.create("ws://" + ip + ":" + port);
            webSocketClient = new SocketClient(uri);
        }
        try {
            webSocketClient.connectBlocking();
            return true;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void release() {
        disConnect();
        webSocketClient = null;
    }

    public static void disConnect() {
        if (webSocketClient == null) return;
        if (!webSocketClient.isOpen()) return;
        try {
            webSocketClient.closeBlocking();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void sendMsg(String string) {
        if (webSocketClient == null) return;
        if (!webSocketClient.isOpen())
            reconnectServer();
        try {
            webSocketClient.send(string);
        } catch (WebsocketNotConnectedException e) {
            e.printStackTrace();
        }
    }

    public static boolean reconnectServer() {
        if (webSocketClient == null) return false;
        if (webSocketClient.isOpen()) return true;
        try {
            webSocketClient.reconnectBlocking();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String getServerAddress() {
        InetSocketAddress remoteSocketAddress = webSocketClient.getRemoteSocketAddress();
        return webSocketClient == null ? "" : remoteSocketAddress.getHostString() + ":" + remoteSocketAddress.getPort();
    }

    public static void setListener(ClientListener listener) {
        SocketClient.listener = listener;
    }

    public static class ClientListener {
        public void onOpen(ServerHandshake handShakeData) {

        }

        public void onMessage(String message) {

        }

        public void onClose(int code, String reason, boolean remote) {

        }

        public void onError(Exception ex) {

        }
    }
}
